﻿using System;
using System.Linq;
using Enyim.Caching;
using Ayatta.Contract;
using Newtonsoft.Json;
using Web.My.Models;
using System.Web.Mvc;
using Web.Base.Security;
using Ayatta.Contract.Domain;
using Ayatta.Contract.Component;

namespace Web.My.Controllers
{
    public class AddressController : MyBaseController
    {
        public AddressController(ILogService logService, IBaseService baseService, IExtraService extraService, IStoreService storeService, IOrderService orderService, IGameService gameService, IAuctionService auctionService, IPassportService passportService, IPaymentService paymentService, IMemcachedClient memcachedClient)
            : base(logService, baseService, extraService, storeService, orderService, gameService, auctionService, passportService, paymentService, memcachedClient)
        {
        }

        public ActionResult Index()
        {
            var model = new AddressControllerModel.Index();
            var identity = User.Identity.Identity();

            var addresses = GetUserAddresses(identity.Id).Where(o => o.Category == AddressCategory.Receive).ToList();

            model.Addresses = addresses;

            var temp = addresses.Select(o => new { o.Id, o.Name, o.Company, o.AreaId, o.Address, o.PostalCode, o.Phone, o.Mobile, o.IsDefault }).ToList();
            //model.JsonData = JSON.Instance.ToJSON(temp, new JSONParameters { UseExtensions = false, EnableAnonymousTypes = true });
            model.JsonData = JsonConvert.SerializeObject(temp);
            return View(model);
        }

        public ActionResult Address(int? param)
        {
            var model = new UserAddress();
            var identity = User.Identity.Identity();

            if (param.HasValue && param.Value > 0)
            {
                model = PassportService.GetUserAddress(param.Value, identity.Id);
            }
            return PartialView(model);
        }

        [HttpPost]
        public ActionResult Address(int? param, UserAddress userAddress)
        {

            var result = new SkResult<object>();
            var identity = User.Identity.Identity();
            if (param.HasValue && param.Value > 0)
            {
                if (userAddress.AreaId.Length == 6)
                {
                    var areas = GetAreas();
                    var temp = GetAreaNames(userAddress.AreaId, areas);
                    var old = PassportService.GetUserAddress(param.Value, identity.Id);
                    if (TryUpdateModel(old))
                    {
                        old.Province = temp.Item1;
                        old.City = temp.Item2;
                        old.District = temp.Item3;

                        var status = PassportService.UpdateUserAddress(old);
                        if (status)
                        {
                            result.Status = true;
                        }
                        else
                        {
                            result.Message = "更新收货地址失败！";
                        }
                    }
                }
                else
                {
                    result.Message = "请填写正确的邮政邮码！";
                }
            }
            else
            {
                if (userAddress.AreaId.Length == 6)
                {
                    var areas = GetAreas();
                    var address = new UserAddress();
                    var pcd = GetAreaNames(userAddress.AreaId, areas);

                    address.UserId = identity.Id;
                    address.Category = AddressCategory.Receive;
                    address.Name = userAddress.Name;
                    address.Company = userAddress.Company;
                    address.CountryId = 86;
                    address.AreaId = userAddress.AreaId;
                    address.Province = pcd.Item1;
                    address.City = pcd.Item2;
                    address.District = pcd.Item3;
                    address.Address = userAddress.Address;
                    address.PostalCode = userAddress.PostalCode;
                    address.Phone = userAddress.Phone;
                    address.Mobile = userAddress.Mobile;
                    address.IsDefault = userAddress.IsDefault;
                    address.CreatedBy = User.Identity.Name;
                    address.CreatedOn = DateTime.Now;
                    var status = PassportService.CreateUserAddress(address);
                    if (status)
                    {
                        result.Status = true;
                    }
                    else
                    {
                        result.Message = "添加收货地址失败！";
                    }
                }
                else
                {
                    result.Message = "请填写正确的邮政邮码！";
                }
            }
            return Json(result);
        }

        [HttpPost]
        public ActionResult Delete(int? param)
        {
            var result = new SkResult<string>(false);
            var identity = User.Identity.Identity();

            if (param.HasValue && param.Value > 0)
            {
                var status = PassportService.DeleteUserAddress(param.Value, identity.Id);
                if (status)
                {
                    result.Status = true;
                }
                else
                {
                    result.Message = "删除失败，请重试！";
                }
            }
            return Json(result);
        }
        [HttpPost]
        public ActionResult SetDefault(int? param)
        {
            var result = new SkResult<string>(false);
            var identity = User.Identity.Identity();

            if (param.HasValue && param.Value > 0)
            {
                var status = PassportService.SetDefaultUserAddress(param.Value, identity.Id, AddressCategory.Receive);
                if (status)
                {
                    result.Status = true;
                }
                else
                {
                    result.Message = "设置失败，请重试！";
                }
            }
            return Json(result);
        }

    }
}
